package org.vectrics.struts;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.db.RecordObject;

/**
 * @author mmoore
 */
public class UserBufferManager {
	
    private transient final Logger log = Logger.getLogger(this.getClass());
    public int maximumSize = 10;
    public static final String SESSION_BUFFERED_OBJECT_ATTR_KEY = "bufferedObjects";
    
    public List getBufferedObjects(Request request) {
        log.debug("getBufferedObjects() - session id = " + request.getSessionId());
        List bufferedObjects = (List)request.getSessionAttribute(SESSION_BUFFERED_OBJECT_ATTR_KEY);
        if (bufferedObjects == null) {
            log.debug("No existing buffered objects collection found on session, creating...");
            bufferedObjects = new ArrayList();
            setBufferedObjects(request, bufferedObjects);
        } else {
            log.debug("Found existing buffered objects collection");
        }
        return(bufferedObjects);
    }

    public void setBufferedObjects(Request request, Collection objects) {
        log.debug("setBufferedObjects() - session id = " + request.getSessionId());
        request.setSessionAttribute(SESSION_BUFFERED_OBJECT_ATTR_KEY, objects);
    }

    public RecordObject findObject(Request request, Long recordId) {
        log.debug("findObject() - session id = " + request.getSessionId());
        RecordObject foundObject = null;
        Collection objects = this.getBufferedObjects(request);
        Iterator iter = objects.iterator();
        while (iter.hasNext()) {
            RecordObject object = (RecordObject)iter.next();
            if (object.getId().equals(recordId)) {
                foundObject = object;
                break;
            }
        }
        return(foundObject);
    }
    
    public void addBufferedObject(Request request, RecordObject object) {
        if (object.getId() == null)
            throw new SystemException("Exception, buffered object has NULL id");
        if (findObject(request, object.getId()) == null) {
            log.debug("Object NOT already in buffer, adding...");
            getBufferedObjects(request).add(object);

            if (getBufferedObjects(request).size() > this.maximumSize) {
                getBufferedObjects(request).remove(0);
            }
        } else {
            log.debug("Object already in buffer");
        }
   }
    
    public void removeBufferedObject(Request request, RecordObject objectToAdd) {
        log.debug("removeBufferedObject()");
        Collection objects = this.getBufferedObjects(request);
        Iterator iter = objects.iterator();
        while (iter.hasNext()) {
            RecordObject object = (RecordObject)iter.next();
            if (object.getId().equals(objectToAdd.getId())) {
                objects.remove(object);
                break;
            }
        }
    }
    
    /**
     * @return Returns the maximumSize.
     */
    public int getMaximumSize() {
        return this.maximumSize;
    }
    
    /**
     * @param maximumSize The maximumSize to set.
     */
    public void setMaximumSize(int maximumSize) {
        this.maximumSize = maximumSize;
    }
    
}
